<!-- 小区台账 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入小区名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="所属街道" prop="street">
        <el-select v-model="queryParams.street" placeholder="请选择所属街道" clearable>
          <el-option v-for="dict in dict.type.bus_neighborhoodledger" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="所属班组" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择班组" clearable>
          <el-option v-for="item in teams" :key="item.id" :label="item.teamName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属人员" prop="person">
        <el-select v-model="queryParams.person" placeholder="请选择所属人员" clearable>
          <el-option v-for="dict in dict.type.bus_person_name" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:ledger:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:ledger:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:ledger:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:ledger:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ledgerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="100" align="center" v-if="getColumn('index')">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="小区id" align="center" prop="id" v-if="getColumn('id')" /> -->
      <el-table-column label="小区名称" align="center" prop="busNeighborhoodLedger.name" v-if="getColumn('name')" />

      <el-table-column label="所属街道" align="center" prop="busNeighborhoodLedger.street" v-if="getColumn('street')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_neighborhoodledger" :value="scope.row.busNeighborhoodLedger.street" />
        </template>
      </el-table-column>
      <el-table-column label="所属班组" align="center" prop="busNeighborhoodLedger.team" v-if="getColumn('team')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_person_group" :value="scope.row.busNeighborhoodLedger.team" />
        </template>
      </el-table-column>
      <el-table-column label="所属人员" align="center" prop="busNeighborhoodLedger.person" v-if="getColumn('person')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_person_name" :value="scope.row.busNeighborhoodLedger.person" />
        </template>
      </el-table-column>
      <el-table-column label="引入管数量" align="center" prop="busNeighborhoodLedger.pipeNum" v-if="getColumn('pipeNum')" />
      <el-table-column label="相邻管沟数量" align="center" prop="busNeighborhoodLedger.nearPipeNum" v-if="getColumn('nearPipeNum')" />
      <el-table-column label="庭院管长度(m)" align="center" prop="busNeighborhoodLedger.allPipeLength" v-if="getColumn('allPipeLength')" />
      <el-table-column label="架空管长度(m)" align="center" prop="busNeighborhoodLedger.overheadPipeLength"
        v-if="getColumn('overheadPipeLength')" />
      <el-table-column label="地埋管长度(m)" align="center" prop="busNeighborhoodLedger.buriedPipeLength" v-if="getColumn('buriedPipeLength')" />
      <el-table-column label="地埋管主管径(dn)" align="center" prop="busNeighborhoodLedger.buriedPipeDiameter"
        v-if="getColumn('buriedPipeDiameter')" />
      <el-table-column label="地埋管材质" align="center" prop="busNeighborhoodLedger.buriedPipeMaterial" v-if="getColumn('buriedPipeMaterial')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_buried_pipe_material" :value="scope.row.busNeighborhoodLedger.buriedPipeMaterial" />
        </template>
      </el-table-column>
      <el-table-column label="建设年代" align="center" prop="busNeighborhoodLedger.year" v-if="getColumn('year')" />

      <el-table-column label="是否文明城市" align="center" prop="busNeighborhoodLedger.iscivilized" v-if="getColumn('iscivilized')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_neighborhood_civilized_city" :value="scope.row.busNeighborhoodLedger.iscivilized" />
        </template>
      </el-table-column>
      <el-table-column label="调压设施数量" align="center" prop="pressureNum" v-if="getColumn('pressureNum')" />
      <el-table-column label="缸数量" align="center" prop="vatNum" v-if="getColumn('vatNum')" />
      <el-table-column label="闸数量" align="center" prop="brakeNum" v-if="getColumn('brakeNum')" />
      <el-table-column label="楼栋数" align="center" prop="buildingcounts" v-if="getColumn('buildingcounts')" />


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="getColumn('operate')">
        <template slot-scope="scope">

          <el-button size="mini" type="text" icon="el-icon-view" @click="showbldg(scope.row)">楼栋管理</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:ledger:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:ledger:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :pageSizes="pageSizes" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改小区台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小区名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入小区名称" />
        </el-form-item>
        <el-form-item label="所属街道" prop="street">
          <el-select v-model="form.street" placeholder="请选择所属街道">
            <el-option v-for="dict in dict.type.bus_neighborhoodledger" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属班组" prop="team">
          <el-select v-model="form.team" placeholder="请选择所属班组">
            <el-option v-for="dict in dict.type.bus_person_group" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属人员" prop="person">
          <el-select v-model="form.person" placeholder="请选择所属人员">
            <el-option v-for="dict in dict.type.bus_person_name" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="引入管数量" prop="pipeNum">
          <el-input v-model="form.pipeNum" placeholder="请输入引入管数量" />
        </el-form-item>
        <el-form-item label="相邻管沟数量" prop="nearPipeNum">
          <el-input v-model="form.nearPipeNum" placeholder="请输入相邻管沟数量" />
        </el-form-item>

        <el-form-item label="架空管长度(m)" prop="overheadPipeLength">
          <el-input v-model="form.overheadPipeLength" placeholder="请输入架空管长度(m)" />
        </el-form-item>
        <el-form-item label="地埋管长度(m)" prop="buriedPipeLength">
          <el-input v-model="form.buriedPipeLength" placeholder="请输入地埋管长度(m)" />
        </el-form-item>
        <el-form-item label="地埋管主管径(dn)" prop="buriedPipeDiameter">
          <el-input v-model="form.buriedPipeDiameter" placeholder="请输入地埋管主管径(dn)" />
        </el-form-item>
        <el-form-item label="地埋管材质" prop="buriedPipeMaterial">
           <el-select v-model="form.buriedPipeMaterial" placeholder="请选择地埋管材质">
            <el-option
              v-for="dict in dict.type.bus_buried_pipe_material"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>



       

        <el-form-item label="建设年代" prop="year">
          <el-select v-model="form.year" placeholder="请选择建设年代">
            <el-option v-for="dict in dict.type.bus_neighborhood_year" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="楼栋数" prop="buildingCount">
          <el-input v-model="form.buildingCount" placeholder="请输入楼栋数" />
        </el-form-item>
        <el-form-item label="是否文明城市" prop="iscivilized">
          <el-radio-group v-model="form.iscivilized">
            <el-radio v-for="dict in dict.type.bus_neighborhood_civilized_city" :key="dict.value" :label="dict.value">{{
      dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="调压设施数量" prop="regulatorNum">
          <el-input v-model="form.regulatorNum" placeholder="请输入调压设施数量" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="楼栋管理" :visible.sync="bldgshow" width="900px" append-to-body>
      <!-- <el-text > </el-text>  -->
      小区名: {{ bldgname }}
      <!-- <el-text v-text="bldgname" style="font-size: medium ; color: #000;;"> </el-text> -->
      <bldgVue :neighborId="neighborId" />
    </el-dialog>
  </div>
</template>

<script>
import { listLedger, getLedger, delLedger, addLedger, updateLedger } from "@/api/business/ledger";
import { Teamlist } from "@/api/business/person";
import { listStreet_ledger} from "@/api/business/street_ledger";

import bldgVue from '../bldg/index';

export default {

  name: "Ledger",
  dicts: ['bus_neighborhood_year', 'bus_neighborhood_civilized_city', 'bus_person_name', 'bus_neighborhoodledger', 'bus_person_group', 'bus_buried_pipe_material'],
  components: {
    bldgVue
  },

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区台账表格数据
      ledgerList: [],
      // 弹出层标题
      title: "",
      // 是否添加、修改显示弹出层
      open: false,
      //楼栋管理弹出层
      bldgshow: false,
      // 小区名字
      bldgname: "",
      //动态下拉 班组list
      teams: [],
      // 街道list
      streets: [],
      // 导出 选择的小区id
      neighborId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        street: null,
        team: null,
        person: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "小区名称不能为空", trigger: "blur" }
        ],
        street: [
          { required: true, message: "所属街道不能为空", trigger: "change" }
        ],
        team: [
          { required: true, message: "所属班组不能为空", trigger: "change" }
        ],
        person: [
          { required: true, message: "所属人员不能为空", trigger: "change" }
        ],
      },
      // 列信息
      columns: [
        { key: 'index', label: `序号`, visible: true },
        { key: 'id', label: '序号', visible: true },
        { key: 'name', label: '小区名称', visible: true },
        { key: 'street', label: '所属街道', visible: true },
        { key: 'team', label: '所属班组', visible: true },
        { key: 'person', label: '所属人员', visible: true },
        { key: 'pipeNum', label: '引入管数量', visible: true },
        { key: 'nearPipeNum', label: '相邻管沟数量', visible: true },
        { key: 'allPipeLength', label: '庭院管长度(m)', visible: true },
        { key: 'overheadPipeLength', label: '架空管长度(m)', visible: true },
        { key: 'buriedPipeLength', label: '地埋管长度(m)', visible: true },
        { key: 'buriedPipeDiameter', label: '地埋管主管径(dn)', visible: true },
        { key: 'buriedPipeMaterial', label: '地埋管材质', visible: true },
        { key: 'year', label: '建设年代', visible: true },
        { key: 'iscivilized', label: '是否文明城市', visible: true },
        { key: 'regulatorNum', label: '调压设施数量', visible: true },
        { key: 'vatNum', label: '缸数量', visible: true },
        { key: 'brakeNum', label: '闸数量', visible: true },
        { key: 'buildingCount', label: '楼栋数', visible: true },
        { key: 'pressureNum', label: '调压设施数量', visible: true },
        { key: 'operate', label: `操作`, visible: true },
        { key: 'buildingcounts', label: '楼栋数量', visible: true },
      ],
      // 分页参数
      pageSizes: [10, 50, 100, 500, 1000]
    };
  },
  created() {
    this.getList();
    this.getTeams();
    // this.getStreets();
  },
  methods: {

    // 获取班组下拉框
    async getTeams() {
      await Teamlist().then(response => {
        // console.log('data:', response.rows);
        this.teams = response.rows
      })
       console.log('班组下拉框：', this.teams);
    },
    // 获取街道数据
    // async getStreets() {
    //   await listStreet_ledger().then(response => {
    //     // console.log('data:', response.rows);
    //     this.streets = response.rows
    //   })
    //    console.log('街道下拉框：', this.streets);
    // },
    /** 查询小区台账列表 */
    getList() {
      this.loading = true;
      listLedger(this.queryParams).then(response => {
        this.ledgerList = response.rows;
        console.log('小区数据:', response.rows);

        // 计算庭院管长度 , 
        this.ledgerList.forEach(item => {
          if (item.vatNum == null) {
            item.vatNum = 0;
          }
          if (item.brakeNum == null) {
            item.brakeNum = 0;
          }
          item.busNeighborhoodLedger.allPipeLength = item.busNeighborhoodLedger.overheadPipeLength + item.busNeighborhoodLedger.buriedPipeLength;

        });
        //   添加缸数量和闸数量
        // this.ledgerList = this.ledgerList.map(item => {
        //   return {
        //     ...item,
        //     vatCount: this.neighborhoodQuery(item.id).vatCount,
        //     brakeCount: this.neighborhoodQuery(item.id).brakeCount
        //   };
        // });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        street: null,
        team: null,
        person: null,
        pipeNum: null,
        nearPipeNum: null,
        allPipeLength: null,
        overheadPipeLength: null,
        buriedPipeLength: null,
        buriedPipeDiameter: null,
        buriedPipeMaterial: null,
        year: null,
        buildingCount: null,
        delFlag: null,
        createUser: null,
        createBy: null,
        createTime: null,
        updateUser: null,
        updateBy: null,
        updateTime: null,
        iscivilized: null,
        regulatorNum: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // console.log('默认选中！！');
      // console.log(this.dict.type.bus_neighborhood_year[0].label);
      this.form.year = this.dict.type.bus_neighborhood_year[0].label;
      this.open = true;
      this.title = "添加小区台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.busNeighborhoodLedger.id || this.ids
      console.log('id', id);
      getLedger(id).then(response => {
        
        this.form = response.data;
        this.open = true;
        this.title = "修改小区台账";
      });
    },
    // 展示楼栋管理弹窗
    showbldg(row) {
      // console.log('小区数据',row );
      this.neighborId = row.busNeighborhoodLedger.id;
      console.log('id', this.neighborId);

      let bldgname = row.name;
      // console.log(bldgname);
      this.bldgname = bldgname;
      // console.log('展示楼栋管理弹窗',this.bldgname);

      this.bldgshow = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLedger(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {

            addLedger(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delLedger(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/neighborhoodledger/export', {
        ...this.queryParams
      }, `neighborhoodledger_${new Date().getTime()}.xlsx`)
    },
    /** 列是否显示 */
    getColumn(key) {
      const res = this.columns.filter(item => item.key === key);
      if (res.length >= 1) {
        return res[0].visible
      } else {
        return false
      }
    },
    // 获取小区的缸数量和闸数量   感觉用多表查询更好  意境完成了，在获取list时就查了·
    neighborhoodQuery(id) {
      let 查询结果 = {
        vatCount: 0,
        brakeCount: 0
      };
      return 查询结果;
    }
  }
};
</script>